<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="逻辑删除标记（0：显示；1：隐藏）" prop="isdeleted">
      <el-input v-model="dataForm.isdeleted" placeholder="逻辑删除标记（0：显示；1：隐藏）"></el-input>
    </el-form-item>
    <el-form-item label="客户ID" prop="customerid">
      <el-input v-model="dataForm.customerid" placeholder="客户ID"></el-input>
    </el-form-item>
    <el-form-item label="护理项目ID" prop="itemid">
      <el-input v-model="dataForm.itemid" placeholder="护理项目ID"></el-input>
    </el-form-item>
    <el-form-item label="护理时间" prop="nursingtime">
      <el-input v-model="dataForm.nursingtime" placeholder="护理时间"></el-input>
    </el-form-item>
    <el-form-item label="护理内容" prop="nursingcontent">
      <el-input v-model="dataForm.nursingcontent" placeholder="护理内容"></el-input>
    </el-form-item>
    <el-form-item label="护理数量" prop="nursingcount">
      <el-input v-model="dataForm.nursingcount" placeholder="护理数量"></el-input>
    </el-form-item>
    <el-form-item label="护理人员ID" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="护理人员ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          isdeleted: '',
          customerid: '',
          itemid: '',
          nursingtime: '',
          nursingcontent: '',
          nursingcount: '',
          userid: ''
        },
        dataRule: {
          isdeleted: [
            { required: true, message: '逻辑删除标记（0：显示；1：隐藏）不能为空', trigger: 'blur' }
          ],
          customerid: [
            { required: true, message: '客户ID不能为空', trigger: 'blur' }
          ],
          itemid: [
            { required: true, message: '护理项目ID不能为空', trigger: 'blur' }
          ],
          nursingtime: [
            { required: true, message: '护理时间不能为空', trigger: 'blur' }
          ],
          nursingcontent: [
            { required: true, message: '护理内容不能为空', trigger: 'blur' }
          ],
          nursingcount: [
            { required: true, message: '护理数量不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '护理人员ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/nurserecord/nurserecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.isdeleted = data.nurseRecord.isdeleted
                this.dataForm.customerid = data.nurseRecord.customerid
                this.dataForm.itemid = data.nurseRecord.itemid
                this.dataForm.nursingtime = data.nurseRecord.nursingtime
                this.dataForm.nursingcontent = data.nurseRecord.nursingcontent
                this.dataForm.nursingcount = data.nurseRecord.nursingcount
                this.dataForm.userid = data.nurseRecord.userid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/nurserecord/nurserecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'isdeleted': this.dataForm.isdeleted,
                'customerid': this.dataForm.customerid,
                'itemid': this.dataForm.itemid,
                'nursingtime': this.dataForm.nursingtime,
                'nursingcontent': this.dataForm.nursingcontent,
                'nursingcount': this.dataForm.nursingcount,
                'userid': this.dataForm.userid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
